<?php defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * Description: 订单管理
 *
 * @author      Eden
 * @datetime    2016/12/10 09:22
 * @copyright   Beijing CmsTop Technology Co.,Ltd.
 */

class Lists extends ADM_Controller
{

    const PAGE_SIZE = 20;

    private $orderStatus = array(
        'Unpaid','Pendding','Dispatch','Complete','Returns','Refund','Cancel'
    );

    /**
     * 初始化
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     *
     */
    public function index()
    {
        $this->load->view(parent::TMP_DIR . '/order/index.html');
    }

    /**
     * 获取订单列表
     */
    public function getLists()
    {
        $send['state'] = false;
        do {

            $orderStatus = $this->input->get('status');
            if (!empty($orderStatus) && !in_array($orderStatus, $this->orderStatus)) {
                $send['error'] = '无效订单状态';
                break;
            }

            $page = $this->input->get('page');
            $page = intval($page) > 0 ? intval($page) : 1;
            $offset = ($page - 1) * self::PAGE_SIZE;

            $where = array(
                'orderStatus' => $orderStatus
            );

            $this->load->model('admin/order/Order_Model', 'order');
            $lists = $this->order->getAll($where, $offset, self::PAGE_SIZE);
            $send['state'] = true;
            $send['data'] = empty($lists) ? array(
                'total' => 0,
                'nextPage' => false,
                'data' => array()
            ) : $lists;
        } while(false);
        $this->output->send($send);
    }

    /**
     * 获取订单详情
     */
    public function getDetail()
    {
        $send['state'] = false;
        do {
            try {
                $this->load->model('admin/order/Detail_Model', 'detail');
                $res = $this->detail->getOrderDetail($this->input->get());
                if (!$res) {
                    $error = $this->detail->getError();
                    $send['error'] = !empty($error) ? $error : '订单获取失败';
                }
                $send['state'] = true;
                $send['data'] = $res;
            } catch(Exception $e) {
                $send['error'] = $e->getMessage();
            }
        } while(false);
        $this->output->send($send);
    }

    /**
     * 物流公司信息
     */
    public function getExpress()
    {
        $this->load->model('admin/common/Common_Model', 'common');
        $exp = $this->common->getExpress();
        $send['state'] = true;
        if (empty($exp)) {
            $send['data'] = array();
        } else {
            $send['data'] = $exp;
        }
        $this->output->send($send);
    }

    /**
     * 设置快递
     */
    public function setExpress()
    {
        $send['state'] = false;
        do {

            $params = $this->input->post();
            if (empty($params['orderId']) || empty($params['expressNo'])
                || empty($params['expressName']) || empty($params['simpleName'])) {
                $send['error'] = '参数不完整';
                break;
            }

            if (isset($params['key'])) {
                unset($params['key']);
            }

            $this->load->model('admin/order/Express_Model', 'exp');
            $res = $this->exp->setExpress($params);
            if ($res) {
                $send['state'] = true;
            } else {
                $send['error'] = '设置失败';
            }
        } while(false);
        $this->output->send($send);
    }

    public function getExpressInfo()
    {
        $send['state'] = false;
        do {
            $params = $this->input->get();
            if (empty($params['simpleName']) || empty($params['expressNo'])) {
                $send['error'] = '参数不完整';
                break;
            }

            $this->load->library('util/Util_AliExpress', null, 'AliExpress');
            $res = $this->AliExpress->ask($params['simpleName'], $params['expressNo']);

            if ($res && is_array($res)) {
                $result = value($res, 'showapi_res_body');
                if ($result && isset($result['ret_code']) && $result['ret_code'] == 0) {
                    $send['state'] = true;
                    $send['data'] = array(
                        'expressNo' => $params['expressNo'],
                        'update' => $result['update'],
                        'updateStr' => $result['updateStr'],
                        'tel' => $result['tel'],
                        'expSpellName' => $result['expSpellName'],
                        'expTextName' => $result['expTextName'],
                        'data' => value($result, 'data')
                    );
                    break;
                } else {
                    $send['error'] = !empty($result['msg']) ? $result['msg'] : '暂无跟踪信息';
                }
            } else {
                $send['error'] = '无查询结果';
            }
        } while(false);
        $this->output->send($send);
    }
}
